package com.deuwise.job.customquartz;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.deuwise.common.utils.DateUtils;
import com.deuwise.common.utils.FileHelperUtils;
import com.deuwise.common.utils.SpringContextHolder;
import com.deuwise.job.customquartz.base.JobRun;
import com.deuwise.job.customquartz.base.JobRunLogHelper;
import com.deuwise.job.entity.JobRunLogDo;
import com.deuwise.system.entity.BackUpDo;
import com.deuwise.system.entity.ConfigDo;
import com.deuwise.system.entity.LogDO;
import com.deuwise.system.service.BackUpDoService;
import com.deuwise.system.service.ConfigDoService;
import com.deuwise.system.service.LogDoService;
import org.apache.commons.lang3.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;

import java.util.Date;

/**
 * Copyright
 *
 * @author chenwenchao
 * @date 2019/3/20
 * Description:
 */
public class JobClearLog implements Job,JobRun {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Override
    public void run() {
        ApplicationContext applicationContext = SpringContextHolder.getApplicationContext();
        ConfigDoService doService=SpringContextHolder.getBean(ConfigDoService.class);
        LogDoService logDoService=SpringContextHolder.getBean(LogDoService.class);
        Wrapper<ConfigDo> doWrapper=new EntityWrapper<>();
        doWrapper.eq("code","logClear");
        ConfigDo configDo= doService.selectOne(doWrapper);
        Integer month=0;
        try {
            if(configDo==null){
                logger.error("获取备份周期有误");
                return;
            }
            month=Integer.parseInt(configDo.getValue());
            //清理日志
            Integer days=1;
            if(month>0){
                days=month*30;
            }
            //删除日志文件
            FileHelperUtils.deleteFiles(FileHelperUtils.getLogSavePath(),days);
            Wrapper<LogDO> logDOWrapper=new EntityWrapper<>();
            Date pointDate = new Date();
            pointDate= DateUtils.addDateDays(pointDate,0-days);
            pointDate= DateUtils.endOfDay(pointDate);
            logDOWrapper.le("create_time",pointDate);
            logDoService.delete(logDOWrapper);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("JobClearLog清理日志：执行完毕=======================");
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //添加执行日志
        System.out.println("JobClearLog清理日志：启动任务=======================");
        /*
         * 添加运行日志
         *
         * */
        JobRunLogHelper jobRunLogHelper= SpringContextHolder.getBean(JobRunLogHelper.class);
        JobRunLogDo jobRunLogDo=new JobRunLogDo();
        jobRunLogHelper.getBaseRunLog(context,jobRunLogDo, this.getClass());
    }
}
